from django.contrib import admin
from .models import PricePrediction, DefectDetection, ChatLog, NoteExtraction, ISBNRecognition


@admin.register(PricePrediction)
class PricePredictionAdmin(admin.ModelAdmin):
    """AI价格预测管理"""
    
    list_display = (
        'id', 'book', 'predicted_price_min', 'predicted_price_max', 
        'model_version', 'created_at'
    )
    list_filter = ('created_at', 'model_version')
    search_fields = ('book__title',)
    ordering = ('-created_at',)
    
    fieldsets = (
        ('预测信息', {
            'fields': ('book', 'predicted_price_min', 'predicted_price_max', 'model_version')
        }),
        ('详细信息', {
            'fields': ('factors', 'similar_books')
        }),
    )
    
    readonly_fields = ('created_at',)
    list_per_page = 20
    date_hierarchy = 'created_at'


@admin.register(DefectDetection)
class DefectDetectionAdmin(admin.ModelAdmin):
    """AI瑕疵检测管理"""
    
    list_display = (
        'id', 'book', 'image', 'condition_score', 
        'model_version', 'created_at'
    )
    list_filter = ('created_at', 'model_version')
    search_fields = ('book__title',)
    ordering = ('-created_at',)
    
    fieldsets = (
        ('检测信息', {
            'fields': ('book', 'image', 'condition_score', 'model_version')
        }),
        ('检测结果', {
            'fields': ('detection_result', 'defect_types', 'annotated_image_url')
        }),
    )
    
    readonly_fields = ('created_at',)
    list_per_page = 20
    date_hierarchy = 'created_at'


@admin.register(ChatLog)
class ChatLogAdmin(admin.ModelAdmin):
    """聊天记录管理"""
    
    list_display = (
        'id', 'order', 'sender', 'message_type', 
        'message_preview', 'created_at'
    )
    list_filter = ('message_type', 'created_at')
    search_fields = ('order__order_no', 'sender__username', 'message')
    ordering = ('order', 'created_at')
    
    fieldsets = (
        ('消息信息', {
            'fields': ('order', 'sender', 'message_type', 'message')
        }),
        ('AI解析', {
            'fields': ('ai_parsed_info',),
            'classes': ('collapse',)
        }),
    )
    
    readonly_fields = ('created_at',)
    list_per_page = 30
    date_hierarchy = 'created_at'
    
    def message_preview(self, obj):
        return obj.message[:50] + '...' if len(obj.message) > 50 else obj.message
    message_preview.short_description = '消息内容'


@admin.register(NoteExtraction)
class NoteExtractionAdmin(admin.ModelAdmin):
    """AI笔记提取管理"""
    
    list_display = (
        'id', 'book', 'image', 'note_type', 
        'confidence_score', 'model_version', 'created_at'
    )
    list_filter = ('note_type', 'created_at', 'model_version')
    search_fields = ('book__title', 'extracted_text')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('提取信息', {
            'fields': ('book', 'image', 'note_type', 'model_version')
        }),
        ('提取结果', {
            'fields': ('extracted_text', 'keywords', 'confidence_score')
        }),
    )
    
    readonly_fields = ('created_at',)
    list_per_page = 20
    date_hierarchy = 'created_at'


@admin.register(ISBNRecognition)
class ISBNRecognitionAdmin(admin.ModelAdmin):
    """ISBN识别管理"""
    
    list_display = (
        'id', 'user', 'recognized_isbn', 'confidence_score', 'created_at'
    )
    list_filter = ('created_at',)
    search_fields = ('user__username', 'recognized_isbn')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('识别信息', {
            'fields': ('user', 'image', 'recognized_isbn', 'confidence_score')
        }),
        ('书籍信息', {
            'fields': ('book_info',),
            'classes': ('collapse',)
        }),
    )
    
    readonly_fields = ('created_at',)
    list_per_page = 20
    date_hierarchy = 'created_at'
